<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>管理后台</title>
    <link rel="stylesheet/less" href="../../../static/admin/css/index.less"/>
    <link rel="stylesheet" href="../../../static/admin/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../../static/admin/css/jquery.pagination.css"/>
    <script src="../../../static/admin/js/jquery-1.12.3.js"></script>
    <script src="../../../static/admin/js/less.min.js"></script>
    <script src="../../../static/admin/layui/layui.js"></script>
    <script src="../../../static/layer/layer.js"></script>
</head>
<style>
    .newadd div .adtitle {
        width: 6rem !important;
    }

    .level {
        text-align: left;
    }

    .groupList {
        border-bottom: 1px solid #ebebeb;
        margin-left: 3%;
    }

    .groupList p {
        padding: 1% 0;
    }

    .groupList p input {
        vertical-align: middle;
        margin: 5px;
        width: 18px;
        height: 18px;
    }

    .newadd label {
        padding: 10px 0;
        display: block;
        text-align: left;
    }

    .p1 {
        color: #666;
    }

    .newadd input {
        padding: 0 10px;
        width: 5%;
        height: 30px;
        line-height: 30px;
        border: 1px solid #dcdcdc;
        border-radius: 5px;
    }

    .tj {
        margin-top: 2%;
        text-align: left;
    }

    .tj button {
        width: 2.8rem;
        height: 1.7rem;
        font-size: 13px;
        line-height: 25px;
        margin-left: 1%;
        margin-left: 5%;
    }

</style>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    {% include "/admin/left.html" %}
    <div class="layui-body">
        <div id="box" class="fl">
            <div class="content">
                <h3>用户权限</h3>
                <input id="user_id" type="hidden" value="{{ uid }}" />
                <div class="newadd">
                    <div>
                        <span class="adtitle">用户名称 :</span>
                        <span>{{ res.username }}</span>
                    </div>

                </div>
                <div class="level" id="all_option">
                </div>
                <p class="tj">
                    <button class="groupBtn">提交</button>
                </p>
            </div>
        </div>
    </div>
</div>

</body>
<script>
    layui.use('element', function () {
        var element = layui.element;
    });
    $(function () {
        var html;
        // 读取侧边的所有栏目，并显示在页面上
        $('.layui-side-scroll .layui-nav .layui-nav-item').each(function (index, v) {
            html = "";
            html += "<div class='groupList'>";
            html += "<p>";
            html += "<input type='checkbox' title='' class='val1' name='user'>" + $(this).find('.fa').text();
            html += "</p>";
            html += "<p class='p1'>";
            $($(this).find('dl dd a')).each(function (index_2, v_2) {
                html += "<input type='checkbox' class='ipt' name='val8' value = '" + $(this).text() + "'><span>" + $(this).text() + "</span>";
            });
            html += "</p>";
            html += "</div>";
            $("#all_option").append(html);
        });

        var flag = false;
        $('.val1').on("change", function () {
            flag = $(this)[0].checked;
            $.each($(this).parent().siblings().children(), function (i, v) {
                v.checked = flag
            })
        });

        var user_id = $('#user_id').val();
        // 请求数据，选中该用户已经有的权限
        $.ajax({
            url: '/admin/permission_data',
            type: 'POST',
            data: {
                'user_id': user_id
            },
            success: function (data) {
                if (data.status == 'success') {
                    $.each(data.arr, function (index, va) {
                        $('input[value="' + va['route_info'] + '"]').prop("checked", true);
                    });
                }
            }
        })
    });


    //  提交修改
    $('.groupBtn').click(function () {
        var s = '';
        var obj = $('.ipt');
         for(var i=0; i<obj.length; i++){
            if(obj[i].checked) s+=obj[i].value+','; //如果选中，将value添加到变量s中
        }

        $.ajax({
            url: '/admin/update_per',
            type: 'POST',
            data: {
                'arr': s,
                'cur_id': $('#user_id').val()
            },
            success: function (msg) {
                if (msg.status == 1){
                    layer.msg('修改成功', {time: 2000}, function () {
                    location.href = document.referrer
                })
                }else {
                    layer.msg("操作失败", {time:1200});
                }
            }
        })
    });

</script>
</html>
